package edu.nf.placeorder.common.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wangl
 * @date 2025/10/16
 */
@Configuration
public class RabbitConfig {

    /**
     * 延迟交换机名称
     */
    public static final String DELAY_EXCHANGE = "delay.exchange";
    /**
     * 队列
     */
    public static final String DELAY_QUEUE = "delay.queue";
    /**
     * 路由的key
     */
    public static final String DELAY_KEY = "delay.key";
    /**
     * 延迟投递的时间
     */
    public static final Long DELAY_TIME = 30000L;

    /**
     * 声明延迟交换机
     * @return
     */
    @Bean
    public Exchange delayExchange() {
        //自定义交换机参数
        Map<String, Object> args = new HashMap<>();
        args.put("x-delayed-type", "direct");
        //创建自定义交换机
        return new CustomExchange(DELAY_EXCHANGE,
                "x-delayed-message",
                false, false, args);
    }

    /**
     * 声明队列
     * @return
     */
    @Bean
    public Queue delayQueue() {
        return new Queue(DELAY_QUEUE, false);
    }

    /**
     * 绑定延迟交换机和队列
     * @return
     */
    @Bean
    public Binding delayBinding() {
        return BindingBuilder.bind(delayQueue())
                .to(delayExchange())
                .with(DELAY_KEY)
                .noargs();
    }
}